package com.example.shop_2210.frag

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.example.base.mvvm.BaseFragment
import com.example.shop_2210.R
import com.example.shop_2210.adapter.CarAdapter
import com.example.shop_2210.databinding.FragmentCarBinding
import com.example.shop_2210.fragviewModel.CarViewModel

class CarFragment : BaseFragment<FragmentCarBinding,CarViewModel>() {
    lateinit var carAdapter: CarAdapter
    override fun getLayout(): Int  = R.layout.fragment_car

    override fun getViewModel(): Class<CarViewModel>  = CarViewModel::class.java

    override fun initView() {
        carAdapter = CarAdapter()
        binding.rvCar.adapter = carAdapter
        binding.rvCar.layoutManager = LinearLayoutManager(context)
        //请求购物车数据
        viewModel.getCar("true","1","20")

        carAdapter.addChildClickViewIds(R.id.car_cb,R.id.tv_add,R.id.tv_sub)
        carAdapter.setOnItemChildClickListener { adapter, view, position ->
            when(view.id){
                R.id.car_cb->{
                    //页面会自动变，只需关注数据变化
                    carAdapter.data[position].isChecked = !carAdapter.data[position].isChecked
                }
                R.id.tv_add->{
                    carAdapter.data[position].cartNum++
                    (carAdapter.getViewByPosition(position,R.id.car_num) as TextView).text = "${carAdapter.data[position].cartNum}"
                    viewModel.updateCar(carAdapter.data[position].id,carAdapter.data[position].cartNum)
                }

                R.id.tv_sub->{
                    if(carAdapter.data[position].cartNum>1){
                        carAdapter.data[position].cartNum--
                        (carAdapter.getViewByPosition(position,R.id.car_num) as TextView).text = "${carAdapter.data[position].cartNum}"
                        viewModel.updateCar(carAdapter.data[position].id,carAdapter.data[position].cartNum)
                    }else{
                        //删除放的是数组
                       val ids = arrayListOf<Int>()
                        ids.add(carAdapter.data[position].id)
                        viewModel.deleteCar(ids)
                    }

                }
            }
            //无论点击了那个，都会影响下方控件
            checkAll()
            checkMoney()

        }

        //全选监听
        binding.cb.setOnCheckedChangeListener { buttonView, isChecked ->
            binding.cb.setOnClickListener {
                carAdapter.data.forEach {
                    it.isChecked = isChecked
                }
                carAdapter.notifyDataSetChanged()//刷新页面
                checkMoney()//重新算钱
            }
        }

    }

    private fun checkAll() {
        //存储所有数据的选中状态
        val list = mutableListOf<Boolean>()
        carAdapter.data.forEach {
            list.add(it.isChecked)
        }
        if(list.contains(false)){
            binding.cb.isChecked = false
        }else{
            binding.cb.isChecked = true
        }
        //判断集合是否存在未选中的状态  就是未选中
    }

    private fun checkMoney() {
        var money = 0f
        carAdapter.data.forEach {
            if(it.isChecked){
                money+=it.cartNum*it.price.toFloat()
            }
        }
        binding.total.text = "合计$money 元"
    }


    override fun initData() {
        viewModel.getSuccess.observe(this){
            if(it.code==200){
                carAdapter.data.clear()
                carAdapter.data.addAll(it.data.list)
                carAdapter.notifyDataSetChanged()
            }
        }
    }

}
